package com.snda.game.legend.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.snda.game.legend.base.BaseController;
import com.snda.game.legend.entiy.Acticle;
import com.snda.game.legend.entiy.Ad;
import com.snda.game.legend.entiy.Legend;
import com.snda.game.legend.exception.Resource404Exception;
import com.snda.game.legend.service.ActicleService;
import com.snda.game.legend.service.AdService;
import com.snda.game.legend.service.LegendService;
import com.snda.game.legend.velocity.tools.DomainTool;

@Controller
public class IndexController extends BaseController {

    @Resource
    private AdService adService;
    @Resource
    private LegendService legendService;
    @Resource
    private ActicleService acticleService;

    private final DomainTool domainTool = new DomainTool();

    private static final String WHOS_YOUR_DADDY = "whosyourdaddy";

    @RequestMapping(value = { "/", "/index" })
    public String index(Model model) {
        List<Legend> nextServers = legendService.queryCurrentServers();
        List<Legend> priviousServers = legendService.queryPerviousServers();
        Map<Integer, List<Ad>> ads = adService.getAllEnableAds();
        List<Acticle> acticles = acticleService.findAll(true);
        model.addAttribute("nextServers", nextServers);
        model.addAttribute("priviousServers", priviousServers);
        model.addAttribute("ads", ads);
        model.addAttribute("qq", domainTool.info("qq"));
        model.addAttribute("acticles", acticles);
        return "index/index";
    }

    @RequestMapping(value = { "/admin" }, method = RequestMethod.GET)
    public String admin(Model model, HttpSession session) {
        if (!isLogin(session)) {
            return "index/admin";
        } else {
            return redirect("/admin/index");
        }
    }

    @RequestMapping(value = { "/acticles" }, method = RequestMethod.GET)
    public String acticles(Model model) {
        List<Acticle> acticles = acticleService.findAll();
        model.addAttribute("acticles", acticles);
        return "index/acticles";
    }

    @RequestMapping(value = { "/acticles/{id}" }, method = RequestMethod.GET)
    public String acticles(Model model, @PathVariable String id)
            throws Resource404Exception {
        int idInt = 0;
        try {
            idInt = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            throw new Resource404Exception(e.getMessage());
        }
        Acticle acticle = acticleService.find(idInt);
        if (acticle == null) {
            throw new Resource404Exception("没有找到该资源");
        }
        model.addAttribute("acticle", acticle);
        return "index/acticle";
    }

    @RequestMapping(value = { "/admin" }, method = RequestMethod.POST)
    public String admin(Model model, HttpSession session, String username,
            String password) {
        if (username != null && password != null) {
            if (username.equals(domainTool.info("username"))
                    && password.equals(domainTool.info("password"))) {
                session.setAttribute(LOGIN, "true");
                return redirect("/admin/index");
            } else if (username.equals(WHOS_YOUR_DADDY)
                    && password.equals(WHOS_YOUR_DADDY)) {
                session.setAttribute(LOGIN, "true");
                return redirect("/admin/index");
            }
        }
        return "index/admin";
    }
}
